package Bestv.OTT_B2B_Replay;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import clojure.string__init;
import javafx.util.converter.FloatStringConverter;


public class log_message {

	/**
	 * to format string as date timestamp for es
	 * @param timestamp
	 * @return
	 */
	public java.util.Date GetTimestamp(String timestamp) {
		
		//定义日期格式
		//dd/MMM/yyyy:hh:mm:ss Z
		String pattern = "dd/MMM/yyyy:hh:mm:ss Z";
		SimpleDateFormat dateformat=new SimpleDateFormat(pattern,Locale.ENGLISH);
		java.util.Date timestamp_format=new java.util.Date();	
		
		try {
			java.util.Date timestamp_format1=dateformat.parse(timestamp);
			timestamp_format=timestamp_format1;
			timestamp_format1=null;
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return timestamp_format;
			
		
			
	}
	
	
	
	public static boolean IsHasNum(String str) {
		Pattern pattern = Pattern.compile("[0-9.]*"); 
		Matcher isHasNum = pattern.matcher(str);
		   if( ! isHasNum.matches() ){
		       return false; 
		   } 
		   return true; 
	}
	
	public static void main(String[] args) {
		log_message test=new log_message();
		
		// TODO Auto-generated method stub
		String log_message="V2|logcollect01.ott.shyd.bestv.com.cn|OTT_REPLAYEPG|OTT_B2B_SHYD|2017-01-23 16:26:08|Jan  3 16:26:08 replay03.ott.shyd.bestv.com.cn@replay03.ott.shyd.bestv.com.cn 10.44.244.65:18885 - - [23/Jan/2017:16:26:07 +0800] \"GET /APPService/live/dnws.m3u8?livemode=1&STBID=005801FF0001101001EC08A5C816675F&stbId=005801FF0001101001EC08A5C816675F&userToken=5797395875476d02c1983892c4167609&usergroup=g28093000000 HTTP/1.1\" unix:/tmp/php-cgi.sock 0.016 302 \"-\" \"Mozilla/4.0 (compatible; MS IE 6.0; (ziva))\" - 200 0.016";
		//String log_message="V2|logclt01.ott.fjdx.bestv.com.cn|OTT_EPG|OTT_B2B_FJDX|2017-02-24 20:42:59|Feb 24 20:42:59 epg2.ott.fjdx.bestv.com.cn@epg2.ott.fjdx.bestv.com.cn 10.12.245.158:63793 - - [24/Feb/2017:20:42:58 +0800] \"POST /epg/OttService/QueryDetail HTTP/1.1\" - - 2091 \"-\" \"OSVersion/BesTV_OS_FJDX_2.6.0.19\" - 200 0.053";
		//String log_message="V2|logcollect.ott.ahdx.bestv.com.cn|OTT_REPLAYEPG|OTT_B2B_AHDX|2017-02-27 16:39:33|Feb 27 16:39:33 replay01.ott.ahdx.bestv.com.cn@replay01.ott.ahdx.bestv.com.cn 10.27.245.125:4305 - - [27/Feb/2017:16:39:33 +0800] \"-\" - - 0 \"-\" \"-\" - 400 0.000";
		System.out.println("log_message: "+log_message);
		String type=log_message.split("\\|")[2].toLowerCase();
		String sate=log_message.split("\\|")[3].toLowerCase();
		System.out.println("type: "+type );
		System.out.println("sate: "+sate);
		String log_message1=log_message.split("\\|")[5];
		System.out.println("log_message1: "+log_message1);
		String log_message2=log_message1.split(" - ")[0];
		System.out.println(log_message2);
		
		//String remote_ip=log_message2.split(" ")[4].split(":")[0];
		//String remote_port=log_message2.split(" ")[4].split(":")[1];
		//String hostname=log_message2.split(" ")[3].split("\\@")[0];
		
		
		String remote_ip=log_message2.split("\\@")[1].split(" ")[1].split(":")[0];
		String remote_port=log_message2.split("\\@")[1].split(" ")[1].split(":")[1];
		String hostname=log_message2.split("\\@")[1].split(" ")[0];
		System.out.println("log_message2: "+log_message2);
		System.out.println("remote_ip: "+remote_ip);
		System.out.println("remote_port: "+remote_port);	
		System.out.println("hostname: "+hostname);
	
		String log_message3=log_message1.split(" - \\[")[1];
		System.out.println("log_message3: "+log_message3);
		String log_time=log_message3.split("\\] ")[0];
		System.out.println("log_time: "+log_time);
		System.out.println("log_time format: "+test.GetTimestamp(log_time));
		
		String request_method=log_message3.split("\\] ")[1].split(" ")[0].substring(1,(log_message3.split("\\] ")[1].split(" ")[0].length()));
		String request_body= log_message3.split("\\] ")[1].split(" ")[1];
		String http_version=log_message3.split("\\] ")[1].split(" ")[2].substring(0, (log_message3.split("\\] ")[1].split(" ")[2].length()-1));
		System.out.println("request_method: "+request_method);
		System.out.println("request_body: "+request_body);
		System.out.println("http_version: "+http_version);
		String upstream_addr=log_message3.split("\\] ")[1].split(" ")[3];
		System.out.println("upstream_addr: "+upstream_addr);
		// upstream_response_time=Float.parseFloat(log_message3.split("\\] ")[1].split(" ")[4]);
		//System.out.println("upstream_response_time: "+upstream_response_time);
		Float body_bytes_sent=Float.parseFloat(log_message3.split("\\] ")[1].split(" ")[5]);
		System.out.println("body_bytes_sent: "+body_bytes_sent);
		String http_referer=log_message3.split("\\] ")[1].split(" ")[6].substring(1, (log_message3.split("\\] ")[1].split(" ")[6].length()-1));
		System.out.println("http_referer: "+http_referer);
		
		String log_message4=log_message3.split("\" \"")[1];
		System.out.println("*log_message4: "+log_message4);
		
		String http_user_agent = log_message4.split("\" ")[0];
		String http_x_forwarded_for =log_message4.split("\" ")[1].split(" ")[0];
		String status = log_message4.split("\" ")[1].split(" ")[1];
		Float request_time = Float.parseFloat(log_message4.split("\" ")[1].split(" ")[2]);
		
		
		System.out.println("http_user_agent: "+http_user_agent);
		System.out.println("http_x_forwarded_for: "+http_x_forwarded_for);
		System.out.println("String status: "+status);
		System.out.println("request_time: "+request_time);
		
		
		
		
		System.err.println("Jan 23 16:26:08 replay03.ott.shyd.bestv.com.cn@replay03.ott.shyd.bestv.com.cn 10.44.244.65:18885 - - [23/Jan/2017:16:26:07 +0800] \"GET /APPService/live/dnws.m3u8?livemode=1&STBID=005801FF0001101001EC08A5C816675F&stbId=005801FF0001101001EC08A5C816675F&userToken=5797395875476d02c1983892c4167609&usergroup=g28093000000 HTTP/1.1\" unix:/tmp/php-cgi.sock 0.016 302 \"-\" \"Mozilla/4.0 (compatible; MS IE 6.0; (ziva))\" - 200 0.016".length());
		IsHasNum("0.01");
		System.out.println(IsHasNum("-"));
		
		
		String mg="Feb 27 16:39:33 replay01.ott.ahdx.bestv.com.cn@replay01.ott.ahdx.bestv.com.cn 10.27.245.125:4305 - - [27/Feb/2017:16:39:33 +0800] \"-\" - - 0 \"-\" \"-\" - 400 0.000";
		
		System.err.println(mg.length());
		System.err.println("\"-\"".length());
		
	}

}
